//
//  SAFRequestApi.m
//  TSBanPai
//
//  Created by wandong on 2017/10/17.
//  Copyright © 2017年 tingshan. All rights reserved.
//

#import "SAFRequestApi.h"
#import "SAFNetworking.h"
#import "SAPI.h"
#define RESPONSE_FAIL_COMMON_HANDLE [HudBar showMsg:@"连接服务器失败"];

#define RESPONSE_ERROR_COMMON_HANDLE if (failureBlock) {\
failureBlock(responseObject);\
}\
if (![api isEqualToString:@"login_login"] && [responseObject[@"msg"] isEqualToString:@"login_out"]) {\
[SAFRequestApi loginBackground];\
}else{\
[HudBar showMsg:responseObject[@"msg"]];\
}

//不带动画
#define AF_POST_API_FUNC(apiPara) [SAFRequestApi commonFunc:([SAPI api:(apiPara)]) postPara:submitData successBlock:successBlock failureBlock:failureBlock];
#define AF_GET_API_FUNC(apiPara) [SAFRequestApi commonFunc:([SAPI api:(apiPara)]) getPara:submitData successBlock:successBlock failureBlock:failureBlock];
//带动画
#define AF_POST_API_ANIMATION_FUNC(apiPara) [SAFRequestApi commonFuncWithAnimation:([SAPI api:(apiPara)]) postPara:submitData successBlock:successBlock failureBlock:failureBlock];
#define AF_GET_API_ANIMATION_FUNC(apiPara) [SAFRequestApi commonFuncWithAnimation:([SAPI api:(apiPara)]) getPara:submitData successBlock:successBlock failureBlock:failureBlock];
//上传文件 带动画
#define AF_POSTFILE_API_ANIMATION_FUNC(apiPara) [SAFRequestApi commonFuncWithAnimation:([SAPI api:(apiPara)]) postFilePara:submitData file:data successBlock:successBlock failureBlock:failureBlock progressBlock:progressBlock];
//上传文件+filename 带动画
#define AF_POSTFILE_API_ANIMATION_FILENAME_FUNC(apiPara) [SAFRequestApi commonFuncWithAnimation:([SAPI api:(apiPara)]) postFilePara:submitData filename:filename file:data successBlock:successBlock failureBlock:failureBlock progressBlock:progressBlock];

//直接传URL
//不带动画
#define AF_POST_URL_FUNC(url) [SAFRequestApi commonFunc:(url) postPara:submitData successBlock:successBlock failureBlock:failureBlock];
#define AF_GET_URL_FUNC(url) [SAFRequestApi commonFunc:(url) getPara:submitData successBlock:successBlock failureBlock:failureBlock];
//带动画
#define AF_POST_URL_ANIMATION_FUNC(url) [SAFRequestApi commonFuncWithAnimation:(url) postPara:submitData successBlock:successBlock failureBlock:failureBlock];
#define AF_GET_URL_ANIMATION_FUNC(url) [SAFRequestApi commonFuncWithAnimation:(url) getPara:submitData successBlock:successBlock failureBlock:failureBlock];
//上传文件 带动画
#define AF_POSTFILE_URL_ANIMATION_FUNC(url) [SAFRequestApi commonFuncWithAnimation:(url) postFilePara:submitData file:data successBlock:successBlock failureBlock:failureBlock progressBlock:progressBlock];
//上传文件+filename 带动画
#define AF_POSTFILE_URL_ANIMATION_FILENAME_FUNC(url) [SAFRequestApi commonFuncWithAnimation:(url) postFilePara:submitData filename:filename file:data successBlock:successBlock failureBlock:failureBlock progressBlock:progressBlock];

@implementation SAFRequestApi
/**************************************************************************/
//不带缓冲动画
+ (void)commonFunc:(NSString*)api postPara:(NSDictionary*)submitData successBlock:(SActionBlock1)successBlock failureBlock:(SActionBlock1)failureBlock{
    
    [SAFNetworking POSTJSON:api Paramter:submitData success:^(NSDictionary *responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 0) {
            if (successBlock) {
                successBlock(responseObject);
            }
        }else{
            RESPONSE_ERROR_COMMON_HANDLE
        }
    } failure:^(NSURLResponse *response, NSError *error) {
        if (failureBlock) {
            failureBlock(nil);
        }else{
            RESPONSE_FAIL_COMMON_HANDLE
        }
    }];
}

+ (void)commonFunc:(NSString*)api getPara:(NSDictionary*)submitData successBlock:(SActionBlock1)successBlock failureBlock:(SActionBlock1)failureBlock{
    
    [SAFNetworking GETJSON:api Paramter:submitData success:^(NSDictionary *responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 0) {\
            if (successBlock) {\
                successBlock(responseObject);\
            }\
        }else{\
            RESPONSE_ERROR_COMMON_HANDLE
        }
    } failure:^(NSURLResponse *response, NSError *error) {
        if (failureBlock) {
            failureBlock(nil);
        }else{
            RESPONSE_FAIL_COMMON_HANDLE
        }
    }];
}

//带缓冲动画的
+ (void)commonFuncWithAnimation:(NSString*)api getPara:(NSDictionary*)submitData successBlock:(SActionBlock1)successBlock failureBlock:(SActionBlock1)failureBlock{
    
    [HudBar waitingMsg];
    [SAFNetworking GETJSON:api Paramter:submitData success:^(NSDictionary *responseObject) {
        [HudBar hideWaiting];
        if ([responseObject[@"code"] integerValue] == 0) {\
            if (successBlock) {\
                successBlock(responseObject);\
            }\
        }else{\
            RESPONSE_ERROR_COMMON_HANDLE
        }
    } failure:^(NSURLResponse *response, NSError *error) {
        [HudBar hideWaiting];
        if (failureBlock) {
            failureBlock(nil);
        }else{
            RESPONSE_FAIL_COMMON_HANDLE
        }
    }];
}
+ (void)commonFuncWithAnimation:(NSString*)api postPara:(NSDictionary*)submitData successBlock:(SActionBlock1)successBlock failureBlock:(SActionBlock1)failureBlock{
    
    [HudBar waitingMsg];
    [SAFNetworking POSTJSON:api Paramter:submitData success:^(NSDictionary *responseObject) {
        [HudBar hideWaiting];
        if ([responseObject[@"code"] integerValue] == 0) {\
            if (successBlock) {\
                successBlock(responseObject);\
            }\
        }else{\
            RESPONSE_ERROR_COMMON_HANDLE
        }
    } failure:^(NSURLResponse *response, NSError *error) {
        [HudBar hideWaiting];
        if (failureBlock) {
            failureBlock(nil);
        }else{
            RESPONSE_FAIL_COMMON_HANDLE
        }
    }];
}
//上传文件
+ (void)commonFuncWithAnimation:(NSString*)api postFilePara:(NSDictionary*)submitData file:(id)data successBlock:(SActionBlock1)successBlock failureBlock:(SActionBlock1)failureBlock progressBlock:(SActionBlock1)progressBlock{
    [SAFRequestApi commonFuncWithAnimation:api postFilePara:submitData filename:nil file:data successBlock:successBlock failureBlock:failureBlock progressBlock:progressBlock];
}
//上传文件+filename
+ (void)commonFuncWithAnimation:(NSString*)api postFilePara:(NSDictionary*)submitData filename:(NSString*)filename file:(id)data successBlock:(SActionBlock1)successBlock failureBlock:(SActionBlock1)failureBlock progressBlock:(SActionBlock1)progressBlock{

    if (!progressBlock) {
        [HudBar showPercent:@"处理中..."];
    }
    
    [SAFNetworking UPLOADDATA:api Paramter:submitData filename:filename file:data success:^(NSDictionary *responseObject) {
        if (!progressBlock) {
            [HudBar hideWaiting];
        }
        if ([responseObject[@"code"] integerValue] == 0) {\
            if (successBlock) {\
                successBlock(responseObject);\
            }\
        }else{\
            RESPONSE_ERROR_COMMON_HANDLE
        }
    } failure:^(NSURLResponse *response, NSError *error) {
        if (!progressBlock) {
            [HudBar hideWaiting];
        }
        if (failureBlock) {
            failureBlock(nil);
        }else{
            RESPONSE_FAIL_COMMON_HANDLE
        }
    } progress:^(id res) {
        if (progressBlock) {
            progressBlock(res);
        }else{
            [HudBar setHudProgressObject:res];
        }
    }];
}

//出现密码错误的问题是因为取到了其他账号的密码
+ (void)loginBackground{
//    if (!(useroc.phone&&useroc.passwd)) {
//        [HudBar showMsg:@"自动登录失败，请重新登录"];
//        [[NSNotificationCenter defaultCenter]postNotificationName:ConstantsInstance.UserKickoutNoti object:nil];
//        return;
//    }
//    NSMutableDictionary *dic = [[NSMutableDictionary alloc]initWithDictionary:@{
//                                                                                @"phone":[useroc phone],
//                                                                                @"password":[useroc passwd]
//                                                                                }];
//    if ([[useroc phone] isEqualToString:@"18000000001"] || [[useroc phone] isEqualToString:@"18000000002"]) {
//        [dic setValue:@"123123" forKey:@"password"];
//    }
//    [SAFRequestApi login_login:dic successBlock:^(id res) {
//        NSDictionary *info = res[@"data"];
//        [useroc setRdsession:info[@"rdsession"]];
//        [[SAFNetworking shareInstance].requestSerializer setValue:info[@"rdsession"] forHTTPHeaderField:@"rdsession"];
//        useroc = [UserModelOC yy_modelWithDictionary:info];
//        [HudBar showMsg:@"登录成功，请重新操作"];
//    } failureBlock:^(id res) {
//        [useroc setAutologin:@0];
//        [HudBar showMsg:@"登录失败"];
//        [[NSNotificationCenter defaultCenter]postNotificationName:ConstantsInstance.UserKickoutNoti object:nil];
//    }];
}

/************************************************************************/
//#pragma mark 首页接口
//API_FUNC_NAME1(api_bannerinfo_homePage){
//    AF_POST_API_FUNC(@"api/bannerinfo/homePage")
//}

//#pragma mark 登录
//API_FUNC_NAME1(api_login){
//    AF_POST_API_FUNC(@"api/login")
//}
//
//#pragma mark 注册
//API_FUNC_NAME1(api_register){
//    AF_POST_API_FUNC(@"api/register")
//}
//#pragma mark 注册获取验证码
//API_FUNC_NAME1(api_getRegMsgCode){
//    AF_POST_API_FUNC(@"api/getRegMsgCode")
//}
//#pragma mark 忘记密码验证码
//API_FUNC_NAME1(api_getForgetPasswordMsgCode){
//    AF_POST_API_FUNC(@"api/getForgetPasswordMsgCode")
//}
//#pragma mark 忘记密码时修改密码
//API_FUNC_NAME1(api_forgetPassdUpdatePassword){
//    AF_POST_API_FUNC(@"api/forgetPassdUpdatePassword")
//}
//#pragma mark 认证时验证码
//API_FUNC_NAME1(api_getAuthentication){
//    AF_POST_API_FUNC(@"api/getAuthentication")
//}
//#pragma mark 上传友盟token
//API_FUNC_NAME1(api_push_token){
//    AF_POST_API_FUNC(@"api/push/token")
//}
//#pragma mark 添加订单
//API_FUNC_NAME1(api_order_addOrder){
//    AF_POST_API_FUNC(@"api/order/addOrder")
//}
//#pragma mark 根据用户ID获取地址列表
//API_FUNC_NAME1(api_address_findAddressBuCustomId){
//    AF_POST_API_FUNC(@"api/address/findAddressBuCustomId")
//}

//#pragma mark 获取划拨规则URL
//API_FUNC_NAME1(api_merchant_getRuleURL){
//    AF_POST_API_FUNC(@"api/merchant/getRuleURL")
//}

//#pragma mark 获取用户分润等级
//API_FUNC_NAME1(api_trans_level){
//    AF_GET_API_FUNC(@"api/trans/level")
//}

//#pragma mark 获取终端列表数量
//API_FUNC_NAME1(api_instance_count){
//    AF_GET_API_FUNC(@"api/instance/count")
//}

//#pragma mark 我的终端未激活
//API_FUNC_NAME1(api_instance_list_unActive){
//    AF_GET_API_FUNC(@"api/instance/list/unActive")
//}

//#pragma mark 我的终端审核中
//API_FUNC_NAME1(api_instance_list){
//    AF_GET_API_FUNC(@"api/instance/list")
//}
//
//#pragma mark 我的终端已划拨
//API_FUNC_NAME1(api_instance_flow){
//    AF_GET_API_FUNC(@"api/instance/flow")
//}
//#pragma mark 我的终端可回拨
//API_FUNC_NAME1(api_instance_backFlow){
//    AF_GET_API_FUNC(@"api/instance/backFlow")
//}
//#pragma mark 我的终端去回拨
//API_FUNC_NAME1(api_backFlow_flow){
//    AF_POST_API_FUNC(@"api/backFlow/flow")
//}
//#pragma mark 回拨记录
//API_FUNC_NAME1(api_backFlow_records){
//    AF_GET_API_FUNC(@"api/backFlow/records")
//}

//#pragma mark 回拨历史记录
//API_FUNC_NAME1(api_backFlow_his){
//    AF_GET_API_FUNC(@"api/backFlow/his")
//}

//#pragma mark 划拨POS
//API_FUNC_NAME1(api_pos_flow){
//    AF_POST_API_FUNC(@"api/pos/flow")
//}

//#pragma mark 设置返现金额
//API_FUNC_NAME1(api_merchant_backAmount){
//    AF_POST_API_ANIMATION_FUNC(@"api/merchant/backAmount")
//}

//#pragma mark 激活MPOS
//API_FUNC_NAME1(api_merchant_addMPOS){
//    AF_POST_API_FUNC(@"api/merchant/addMPOS")
//}

//#pragma mark 激活POS
//API_FUNC_NAME1(api_merchant_addPOS1){
//    AF_POST_API_FUNC(@"api/merchant/addPOS1")
//}

//#pragma mark 自助开通商户
//API_FUNC_NAME1(api_merchant_addSimplePOS){
//    AF_POST_API_FUNC(@"api/merchant/addSimplePOS")
//}

//#pragma mark 我要分享
//API_FUNC_NAME1(api_custom_qrCode){
//    AF_POST_API_FUNC(@"api/custom/qrCode")
//}

//#pragma mark 获取课程列表
//API_FUNC_NAME1(api_course_courseList){
//    AF_POST_API_FUNC(@"api/course/courseList")
//}
//
//#pragma mark 活动列表
//API_FUNC_NAME1(api_activity_activityList){
//    AF_POST_API_FUNC(@"api/activity/activityList")
//}
//
//#pragma mark 获取业绩详情
//API_FUNC_NAME1(api_v1_perform_detail){
//    AF_GET_API_ANIMATION_FUNC(@"api/v1/perform/detail")
//}
//#pragma mark 获取业绩首页
//API_FUNC_NAME1(api_custom_v1_performance){
//    AF_GET_API_FUNC(@"api/custom/v1/performance")
//}

//#pragma mark 获取业绩数量
//API_FUNC_NAME1(api_custom_v1_performance_agg){
//    AF_GET_API_FUNC(@"api/custom/v1/performance/agg")
//}

//#pragma mark 我的收益
//API_FUNC_NAME1(api_myEarnings_earnings){
//    AF_POST_API_FUNC(@"api/myEarnings/earnings")
//}
//#pragma mark 历史收益
//API_FUNC_NAME1(api_myEarnings_history){
//    AF_POST_API_FUNC(@"api/myEarnings/history");
//}

//#pragma mark 推广分润
//API_FUNC_NAME1(api_tradeDetail_dividedDetailList){
//    AF_POST_API_FUNC(@"api/tradeDetail/dividedDetailList");
//}

//#pragma mark 了解更多
//API_FUNC_NAME1(api_problems){
//    AF_GET_API_FUNC(@"api_problems")api/problems/v2
//}

//#pragma maek 获取自助报单二维码
//API_FUNC_NAME1(api_merchant_getOrganizationQrCode){
//    AF_POST_API_FUNC(@"api/merchant/getOrganizationQrCode")
//}

//#pragma mark 获取余额
//API_FUNC_NAME1(api_custom_account){
//    AF_GET_API_FUNC(@"api/custom/account")
//}

//#pragma mark 确认验证码
//API_FUNC_NAME1(api_validate_mobile_confirm){
//    AF_GET_API_FUNC(@"api/validate/mobile/confirm")
//}
//#pragma mark 余额支付接口
//API_FUNC_NAME1(api_balance_pay){
//    AF_POST_API_FUNC(@"api/balance/pay")
//}

@end
